package com.syj.collect.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.syj.collect.app.R;
import com.syj.collect.entry.TaskBean;

import java.util.ArrayList;
import java.util.List;

public class MyTaskAdapter extends RecyclerView.Adapter<MyTaskAdapter.MyTaskViewHoder> {

    private Context taskcon;
    private ArrayList<TaskBean.DataBean.ListBean> dList=new ArrayList();

    public MyTaskAdapter(Context context) {
        this.taskcon=context;
    }

    @NonNull
    @Override
    public MyTaskAdapter.MyTaskViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task,parent,false);
        return new MyTaskViewHoder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyTaskAdapter.MyTaskViewHoder holder, int position) {
        holder.tv_name.setText(dList.get(position).getName());
            if (onItemCliklister!=null){
                holder.tv_gotask.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemCliklister.onClick(dList.get(position));

                    }
                });
            }
    }

    @Override
    public int getItemCount() {
        return dList.size();
    }

    OnItemCliklister onItemCliklister;

    public void upDate(List<TaskBean.DataBean.ListBean> str) {
        this.dList.clear();
        this.dList.addAll(str);
        notifyDataSetChanged();
    }


    public  interface  OnItemCliklister{
        void onClick(TaskBean.DataBean.ListBean dataBean);
    }

    public void setOnItemCliklister(OnItemCliklister onItemCliklister){
        this.onItemCliklister=onItemCliklister;
    }

    /**
     *
     */
    class MyTaskViewHoder extends RecyclerView.ViewHolder{
        TextView tv_name;
        TextView tv_gotask;
        public MyTaskViewHoder(@NonNull View itemView) {
            super(itemView);
            tv_name=itemView.findViewById(R.id.tv_name);
            tv_gotask=itemView.findViewById(R.id.tv_gotask);
        }
    }
}
